#!/bin/bash

#Copyright (c) Beijing Wansheng Intelligent Corp. All rights reserved.
#Licensed under the LGPL v3.0 license. 


SCRIPT=$(readlink -f "$0")
script_path=$(cd "$(dirname "$0")/" && pwd)
repo_root_dir=$script_path
platform=host
clean=
pack=
usage ()
{
    echo "[options]"
    echo " -h                                       print help infomation"
    echo " -c                                       clean build"
    echo " -p                                       pack"
    echo " -a [host | arm-a7 | arm64]               set target toolchain"
    exit 1
}

while getopts "cpha:" arg
do
    case $arg in
        a)
        platform=$OPTARG;;
        p)
        pack="yes";;
        c)
        clean="yes";;
        h)
        usage;;
        ?)
        echo $arg
        usage;;
    esac
done


if [ "$clean" = "yes" ]; then
    echo "clean build"
    rm -rf $script_path/out
fi

if [ -z "$WA_SDK_DIR" ]; then
    if [ -z "$WA_IOT_EDGE_SDK_REPO_DIR" ]; then
        echo "WA_IOT_EDGE_SDK_REPO_DIR not set. exit.."
        echo "Please first download wa-iot-edge-sdk repository using command:"
        echo "    git clone https://gitee.com/wasome/wa-iot-edge-sdk.git"
        echo "Then set the WA_IOT_EDGE_SDK_REPO_DIR to the directory of wa-iot-edge-sdk"
        exit 1
    fi
    if [ ! -d "$WA_IOT_EDGE_SDK_REPO_DIR" ]; then
        echo "WA_IOT_EDGE_SDK_REPO_DIR=[$WA_IOT_EDGE_SDK_REPO_DIR], the path not present. exit.."
        exit 1
    fi

    WA_SDK_DIR=$WA_IOT_EDGE_SDK_REPO_DIR/wa-iot-sdk/$platform
fi


if [ ! -d $WA_SDK_DIR ]; then
    echo "Can find sdk path for WA_SDK_DIR"
    exit 1
fi
[ ! -f $WA_SDK_DIR/wa-sdk.cmake ] && echo "No wa-sdk.cmake file under WA_SDK_DIR [$WA_SDK_DIR]. exit.." && exit 1


CMAKE_WA_SDK_DIR="-DWA_SDK_DIR=$WA_SDK_DIR"
echo "WA_SDK_DIR=$WA_SDK_DIR"


platforms_dir=$WA_SDK_DIR/tools/build_env
echo platforms_dir=[$platforms_dir]

if [ "$platform" != "host" ]; then
    toolchainfile=$platforms_dir/toolchain.cmake
    toolchain_def="-DCMAKE_TOOLCHAIN_FILE=$toolchainfile"
   
    if [ -f $platforms_dir/setenv.sh ]; then
        source $platforms_dir/setenv.sh
    fi
fi

echo "toolchain_def=$toolchain_def"

if [ ! -f $repo_root_dir/deps/libmodbus/src/modbus.c ]; then
    git submodule update --init --recursive
    [ $? -eq 0 ] || exit $?
fi

build_dir=$script_path/out/$platform
[ -d $build_dir ] || mkdir -p $build_dir
cd $build_dir
cmd="cmake ../.. -DCMAKE_BUILD_TYPE=Debug $toolchain_def $CMAKE_WA_SDK_DIR -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=$build_dir/bin"
echo $cmd
$cmd
[ $? -eq 0 ] || exit $?
make
[ $? -eq 0 ] || exit $?


echo "Great! Build successful. plugin modules generated in folder:"
echo "   $build_dir/bin"


if [ "$pack" = "yes" ]; then
    [ -d pack ] && rm -r pack
    echo ""
    echo "start to pack the plugins..."
    cd $build_dir
    mkdir -p $build_dir/pack/staging
    mkdir -p $build_dir/pack/staging/$platform

    cp $script_path/pack/package.info ./pack/staging
    cp -r $script_path/pack/ams pack/staging/$platform
    cp -r $script_path/pack/plugin.manifest pack/staging/$platform
    cp $build_dir/bin/*so pack/staging/$platform

    manifest_version=`cat pack/staging/$platform/plugin.manifest |grep "version" | head -n 1`
    echo "manifest_version=$manifest_version"

    pack_version=$(echo "$manifest_version" | awk -F '"' '{print $4}')
    echo "pack_version=$pack_version"
    pattern="^[0-9]+\.[0-9]+\.[0-9]+$"

    if [[ $pack_version =~ $pattern ]]; then
        echo "Version is valid."
    else
        echo "Version got from plugin.manifest is invalid."
        exit 1
    fi

    package_file_name=$build_dir/pack/plugin_modbus_$pack_version
    [ -f $package_file_name ] && rm $package_file_name

    echo "packing from the folder: [$build_dir/pack/staging]"
    tree  $build_dir/pack/staging

    cmd="python3  $WA_SDK_DIR/tools/wa_pack.py -f $build_dir/pack/staging  -o $package_file_name   -v $pack_version"
    echo "cmd=$cmd"
    $cmd

    if [ -f $package_file_name.zip ]; then
        echo "Successful to generate $package_file_name"
    else
        echo "pack failed"
    fi      
fi

